package com.cqjtu.education.controller;

import com.aliyun.oss.OSS;
import com.cqjtu.education.entity.CourseData;
import com.cqjtu.education.service.CourseDataService;
import com.cqjtu.education.utils.AliyunOSS;
import com.cqjtu.education.utils.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * (CourseData)表控制层
 *
 * @author makejava
 * @since 2022-04-20 15:47:45
 */
@RestController
@Api(tags = "课程资料相关接口")
@RequestMapping("courseData")
public class CourseDataController {
    /**
     * 服务对象
     */
    @Resource
    private CourseDataService courseDataService;

    @Resource
    private AliyunOSS aliyunOSS;


    @RequestMapping("getUrl")
    @ApiOperation(value = "getUrl", notes = "获取文件url")
    public Result getUrl(@RequestParam("objectName") String objectName) {
        try {
            return Result.success(aliyunOSS.getUrl(objectName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("获取失败");
    }

    @RequestMapping("/upload")
    @ApiOperation(value = "upload", notes = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "courseData", paramType = "query", value = "文件实体类"),
            @ApiImplicitParam(required = true, name = "file", paramType = "query", value = "上传的文件")
    })
    public Result upload(@RequestPart("courseData") CourseData courseData, @RequestPart("file") MultipartFile file) {
        if (file == null) {
            return Result.fail("文件为空");
        }
        try {
            courseDataService.insert(courseData, file);
            return Result.success(courseData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("上传失败");
    }

    @RequestMapping("/uploadPublic")
    @ApiOperation(value = "uploadPublic", notes = "上传文件设置公共读")
    public Result uploadPublic(@RequestParam("file") MultipartFile file) {
        if (file == null)
            return Result.fail("文件为空");
        try {
            return Result.success(aliyunOSS.uploadPublic(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("上传失败");
    }

    @RequestMapping("/uploadTest")
    @ApiOperation(value = "uploadTest", notes = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "courseData", paramType = "query", value = "文件实体类"),
            @ApiImplicitParam(required = true, name = "file", paramType = "query", value = "上传的文件")
    })
    public Result uploadTest(String courseId, String creator, String parentPath, @RequestPart("file") MultipartFile multipartFile) {
        if (multipartFile == null)
            return Result.fail("文件为空");
        try {
            CourseData courseData = new CourseData();
            courseData.setCourseId(courseId);
            courseData.setCreator(creator);
            courseData.setParentPath(parentPath);
            courseDataService.insert(courseData, multipartFile);
            return Result.success(courseData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("上传失败");
    }


    @RequestMapping("/download")
    @ApiOperation(value = "download", notes = "下载文件")
    public Result download(@ApiParam(required = true, name = "objectName", value = "文件目录路径，以/结尾") @RequestParam("objectName") String objectName, HttpServletResponse response) throws IOException {
        System.out.println(objectName);
        //通知浏览器以附件形式下载
        response.setContentType("application/x-download;charset=utf-8");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(objectName, "utf-8"));
        String detail = aliyunOSS.download(response.getOutputStream(), objectName);
        if (detail.equals("下载失败")) {
            return Result.fail(detail);
        }
        return Result.success(detail);
    }

    //6/22


    @RequestMapping("delete")
    @ApiOperation(value = "delete", notes = "删除文件")
    public Result delete(@RequestParam("dataId") String dataId, @RequestParam("objectName") String objectName) {
        try {
            courseDataService.delete(dataId, objectName);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }

    @RequestMapping("batchDelete")
    @ApiOperation(value = "batchDelete", notes = "批量删除文件")
    @ApiParam(required = true, name = "objectName", value = "文件目录路径的集合，都需以/结尾")
    public Result batchDelete(@RequestParam("dataIdList") List<String> dataIdList, @RequestParam("objectNameList") List<String> objectNameList) {
        try {
            courseDataService.deleteByBatch(dataIdList, objectNameList);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }

    @RequestMapping("deleteFolder")
    @ApiOperation(value = "deleteFolder", notes = "删除文件夹")
    @ApiParam(required = true, name = "objectName", value = "文件目录路径，以/结尾")
    public Result deleteFolder(@RequestParam("dataId") String dataId, @RequestParam("objectName") String objectName) {
        try {
            courseDataService.deleteFolder(dataId, objectName);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }

    @RequestMapping("getObjectList")
    @ApiOperation(value = "getObjectList", notes = "获取文件子目录集合")
    @ApiParam(required = true, name = "objectName", value = "文件目录路径，以/结尾")
    public Result getObjectList(@RequestParam("objectName") String objectName) {
        try {
            return Result.success(courseDataService.queryByParentPath(objectName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("获取失败");
    }


    @RequestMapping("newFolder")
    @ApiOperation(value = "newFolder", notes = "新建文件夹")
    public Result newFolder(@RequestBody CourseData courseData) {
        try {
            courseDataService.newFolder(courseData);
            return Result.success(courseData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("新建文件夹失败");
    }


    @RequestMapping("rename")
    @ApiOperation(value = "rename", notes = "文件重命名")
    public Result rename(@RequestParam("dataId") String dataId, @RequestParam("objectName") String objectName, @RequestParam("newName") String newName) {
        try {
            CourseData courseData = courseDataService.rename(dataId, objectName, newName);
            if (courseData == null) {
                return Result.fail("该目录下有重复名称，请重新命名");
            }
            return Result.success(courseData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("重命名失败");
    }

}

